FROM node:16.2.0 as builder

USER root

# yarn v1 安装私有库的包容易失败，所以这样写。详见 https://github.com/yarnpkg/yarn/issues/7521#issuecomment-564122782
RUN yarn || yarn || yarn && yarn cache clean

COPY ./ /app
WORKDIR /app

RUN npm config set registry https://registry.npmmirror.com
RUN yarn config set registry https://registry.npmmirror.com

# 不要用 CMD ["npm", "run", "serve:${BUILD_ENV}"] ，因为会读不到 BUILD_ENV ，见 https://github.com/moby/moby/issues/5509#issuecomment-42173047
RUN yarn install

RUN npm run build

FROM nginx:stable
USER root
RUN mkdir /app
COPY --from=builder /app/dist /app

# Expose the port for access
EXPOSE 8000

# Run the Nginx server
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
